import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module_common/provider/nav_key.dart';
import 'package:flutter/material.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../module/module_browser/viewmodel/before_browser.dart';
import '../../module/module_create_wallet/pages/set_pass_page.dart';
import '../../module/module_trade/pages/trade_receive.dart';
import '../../module/module_trade/pages/trade_transfer.dart';
import '../provider/token_list_provider.dart';
import '../provider/user_settings_provider.dart';
import '../router/Routers.dart';
import '../widgets/dialog/kai_bottom_tip_dialog.dart';
import 'env.utils.dart';

class TaskCodeToFlutter {
  // 日常任务 跳转 flutter 具体页面 的code
  // 306 mpc 转账
  // 307 MPC 收款
  // 309 MPC 充值
  // 308 申购KaiEarn
  // 303  KaiPay 充值
  // 304 KaiPay 转账
  static List<String> taskCodeList = ["306", "307", "304", "303", "308"];

  static linkToPage(String taskCode,{String toAddress = ''}) {
    if (taskCode == '306') {
      return TaskCodeToFlutter.linkToMPCTransfer(toAddress);
    }
    if (taskCode == '307') {
      return TaskCodeToFlutter.linkToMPCReceive();
    }
    // 304 KaiPay 充值
    if (taskCode == '303') {
      return TaskCodeToFlutter.linkToDAppLink(
          "${EnvUtil.getEnvConfig().kaiPayUri}/");
    }
    // 申购
    if (taskCode == '308') {
      return TaskCodeToFlutter.linkToDAppLink(
          "${EnvUtil.getEnvConfig().kaiEarnUri}/");
    }
  }

  // 306 mpc 转账
  static linkToMPCTransfer(String? toAddress) {
    try {
      var context = NavKey.getContext();
      // 转账
      bool isSetPass = context.read<UserSettingsProvider>().hasPassword();
      if (!isSetPass) {
        Utils.showBottomSheetDialog(
            context,
            KaiBottomTipDialog(
              type: DialogType.PASSWORD,
              Confirm: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => SetPassPage(
                            type: '4',
                          ),
                      settings: RouteSettings(name: Routes.rootSetPassRoute)),
                );
              },
            ));
        return;
      }
      List<Token> _tokenList = ObjectBox()
          .tokenModel
          .getIsAddedTokenListByWalletInfoId(
              context.read<TokenListNotifier>().walletInfoId);
      Token token =
          _tokenList.firstWhere((token) => token.contractType == 'native');
      Navigator.of(context).push(MaterialPageRoute(
          builder: (_) {
            return TradeTransferPage(
              toAddress: toAddress,
              contract: token.contract,
            );
          },
          settings: RouteSettings(name: Routes.transfer)));
    } catch (e) {
      kaiPrint('linkToMPCTransfer ${e.toString()}');
    }
  }

  // 307 MPC 收款
  static linkToMPCReceive() {
    try {
      var context = NavKey.getContext();
      List<Token> _tokenList = ObjectBox()
          .tokenModel
          .getIsAddedTokenListByWalletInfoId(
              context.read<TokenListNotifier>().walletInfoId);
      Token token =
          _tokenList.firstWhere((token) => token.contractType == 'native');
      Navigator.of(context).push(MaterialPageRoute(
          builder: (_) {
            return TradeReceivePage(
                contract: token.contract, contractType: token.contractType);
          },
          settings: RouteSettings(name: Routes.receive),
          maintainState: false));
    } catch (e) {
      kaiPrint('linkToMPCReceive ${e.toString()}');
    }
  }

  // DApp 中的 h5 页面
  static linkToDAppLink(String url) {
    try {
      var context = NavKey.getContext();
      doGotoBrowser(context: context, webUrl: url);
    } catch (e) {
      kaiPrint('linkToMPCReceive ${e.toString()}');
    }
  }
}
